﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using GtsServer.Model;
using GtsServer.Common;

namespace GtsServer.Service.TaskStartEndService
{
    public class TaskRobotController
    {
        //已参加该场比赛的机器人编号
        public HashSet<int> hsUserID = new HashSet<int>();
        public TaskRobotSetting settingModel = new TaskRobotSetting();
        static object objLock = new object();
        MsmqSqlService.MsmqSqlServiceClient client = new MsmqSqlService.MsmqSqlServiceClient();

        Timer scoreTimer = null;
        Timer robotTimer = null;

        int editScoreTimer = 0;
        int editRobotTimer = 0;


        public TaskRobotController(TaskRobotSetting model)
        {
            settingModel = model;
            //积分定时器
            scoreTimer = new Timer(model.ScoreEditTime.Value * 1000);
            scoreTimer.Elapsed += new ElapsedEventHandler(ScoreTimer_Elapsed);
            scoreTimer.Start();
            //机器人定时器
            robotTimer = new Timer(model.RobotAddTime.Value * 1000);
            robotTimer.Elapsed += new ElapsedEventHandler(RobotTimer_Elapsed);
            robotTimer.Start();
        }

        public void EditTime()
        {
            //积分计时器
            if (settingModel.ScoreEditTime == 0)
                scoreTimer.Stop();
            else if (editScoreTimer == 0)
            {
                scoreTimer.Interval = settingModel.ScoreEditTime.Value * 1000;
                scoreTimer.Start();
            }
            else
                scoreTimer.Interval = settingModel.ScoreEditTime.Value * 1000;
            editScoreTimer = settingModel.ScoreEditTime.Value;

            //机器人计时器
            if (settingModel.RobotAddTime == 0)
                robotTimer.Stop();
            else if (editRobotTimer == 0)
            {
                robotTimer.Interval = settingModel.RobotAddTime.Value * 1000;
                robotTimer.Start();
            }
            else
                robotTimer.Interval = settingModel.RobotAddTime.Value * 1000;
            editRobotTimer = settingModel.RobotAddTime.Value;
        }

        /// <summary>
        /// 机器人定时加积分服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ScoreTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            Random rd = new Random();
            lock (objLock)
            {
                foreach (var item in hsUserID)
                {
                    int score = rd.Next(settingModel.MinScore.Value, settingModel.MaxScore.Value);
                    if (score > 0)
                    {
                        //更改机器人分数
                        client.EditRobotTaskScore(settingModel.TaskID.Value, item, score);
                    }
                }
            }
        }

        /// <summary>
        /// 机器人定时加入比赛
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void RobotTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            Random rd = new Random();
            //获取所要加载的机器人数量
            int count = rd.Next(settingModel.RobotMinCount.Value, settingModel.RobotMaxCount.Value);

            while (count > 0)
            {
                if (hsUserID.Count == settingModel.RobotMaxIndex.Value - settingModel.RobotMinIndex.Value)
                    return;
                int userIndex = rd.Next(settingModel.RobotMinIndex.Value, settingModel.RobotMaxIndex.Value);
                int userID = TaskController.dictRobotUser[userIndex].ID;

                //添加机器人
                lock (objLock)
                {
                    if (!hsUserID.Contains(userID))
                    {
                        hsUserID.Add(userID);
                        int score = rd.Next(settingModel.MinScore.Value, settingModel.MaxScore.Value);
                        if (score > 0)
                        {
                            client.EditRobotTaskScore(settingModel.TaskID.Value, userID, score);
                        }
                        count--;
                    }
                }
            }
        }

        /// <summary>
        /// 释放资源
        /// </summary>
        public void Dispose()
        {
            scoreTimer.Stop();
            scoreTimer.Dispose();
            robotTimer.Stop();
            robotTimer.Dispose();
        }
    }
}
